package com.lujianfei.powerinput;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.lujianfei.powerinput.base.BaseActivity;
import com.lujianfei.powerinput.ui.noteList.presenter.NoteListPresenter;
import com.lujianfei.powerinput.ui.noteList.base.NoteListContract;
import com.lujianfei.powerinput.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class MainActivity extends BaseActivity implements NoteListContract.View{
    private final static String TAG = "MainActivity";
    private ListView listview;
    private EmptyView emptyView;
    private ArrayAdapter adapter;
    private final List<String> data=new ArrayList<>();
    private NoteListContract.Presenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setTitleMiddleText("信息录入神器",true);
        showTitleLeftImage();
        showTitleRightImage();
        setTitleLeftImage(R.drawable.ic_setting);
        setTitleRightImage(R.drawable.ic_add);
        initEvent();
        presenter = new NoteListPresenter(this);
    }

    private void initEvent() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.onItemClick(adapterView,view,i,l);
            }
        });
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                presenter.onItemLongClick(adapterView,view,i,l);
                return true;
            }
        });
    }

    private void initView() {
        listview = findViewById(R.id.listview);
        emptyView = findViewById(R.id.emptyView);
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public void onTitleLeftImageClick() {
        presenter.onTitleLeftImageClick();
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        presenter.onTitleRightImageClick();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.initData();
    }


    @Override
    public Activity currentActivity() {
        return this;
    }

    @Override
    public void updateList(List<String> data) {
        if(null == adapter) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.data);
            listview.setAdapter(adapter);
        }
        this.data.clear();
        this.data.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showEmptyView() {
        View parent = (View) emptyView.getParent();
        parent.setVisibility(View.VISIBLE);
        emptyView.setTitleText("开始录入你的信息吧");
        emptyView.showButton();
        emptyView.setButtonText("走你");
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onTitleRightImageClick();
            }
        });
    }

    @Override
    public void hideEmptyView() {
        View parent = (View) emptyView.getParent();
        parent.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }
}
